package com.example.a.entity;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.example.a.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
     @Autowired
     private OrderDetailService orderService;
    //appid
    private final String APP_ID = "9021000149690074";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCjPv4UDwpvnSCXYHPMLHDZR51bQgtzfSKsFYbDLtFO+e3VmToQ3bW0e7ZubmhbnF6fJDBwliGIgJi8QmouErQ1eZR/a4NyY+4fG/3S073LkeeUJ/TbpUOfbQ62Q9ziKnIyx/uOBeepL1C9VwvPVV77VQIGoe86UWIV6ZskJ/J1RE9QnqBRj0TUk5foIrXDDSClK05KLPEM8HVj5AQCJxFyWdjmHDvkjIj+sAKjtFhuRectNPShkN/u6l9MXyP8+8gkk6TXrILOMRxu00ZudL4jD1VCN9z7esK2fgbiIeSxLLm7ikAt6jSwN6jVXtOuywLjn2j4widCVxa0OaCJpDWxAgMBAAECggEAQwpBibdLj961XbVMmNK/XVvdsXaEAV+od4c3mryFVKurEocko3yJfJ85okxRKx4idbSE+BePmeFF2DpmJJmlzDgr1dAvYTDMv5XdPVGbphg7RErMcc+C39wHqK5h7beEwnoCQqcGJiK6Q6eVwAXNReX5OQQTnJ2DbIBNKCIHnkRQv66H2YE5ffENZyhDAdfnwoubsNSOr2hwh8BLsX0WyP1gWVgzUM+KatnfPJoJ8aeXXl5/PGXVx2AfW96DTTFIhistaI0BQMUFXDj9yQuhZ/moumKuqI6gp3tJ9LHwmlU/cb8bsmGKu37obQfo1qHc58SO53qZGzZsV+rwCzPQgQKBgQDfr/2xGz7slkBUw/fw/k1nYYFUq1YvSTS5GPNx/9O6rp4i1PvRcVEB8UHndxlWWmNaCy9hpmFK/MvlrwnbBo1/b+6mY3jZwsBpmX2bpnwH/rZDTQuEbbYVj6NfuboPMRFmLRx21GKrtjpZek8WJGOvVNViX64s0H1V4Lg4+OxsuQKBgQC6092uXEsuCcIK7LdyPkz/E5QTo/kPm/uAyQUe/qWEoS9XuOPgZ9RLRtcnRMqUsfTEIWBR/PugdmjoQXNZoUt5PW09bKofM7+gRgbfGHc7nXEXF/kRR52+HqfO762oOMUTYWUUOzaPamcyItf7zLGsw0LgQ+BS4xVR+QH8MezEuQKBgQDWU0BWk+6ErapSDbrhlCgCKDDNOaXIhPIZcEjCMAdk1a77XOszUMn3fHxTFZCXZt/PBOyPOeX6/aE4H7Dnxa2KL2uQzbqbm7jU9ZaerrQkzO9CKO4iIyTWH9y/+arGp5MuPYcKbJFmSaI63bAtXQyOkFAsg9TVZ6bPxBN/szKFMQKBgQCDATtqH1esUbVaOcEs/9i36H+mAg4MTwFdvhe0MzXXObtb1M8dtVDc9DC8XXV8LQkIWqsS8sVzMHTBn9uF1x94RYWLU947AOt3NINu/tXk3LLHUc0Zu9E11oMvDUwFMMGopFvfguHwUNlKA7rV2yQfk9AaOB5doNR7ZZNpx4xSKQKBgQCril4k54kiG1c5Nr0g8QJbykUzZcDrYNeeHWQECyOYO4pFdWrOquIdv2Ws06NTqlAew2v+Dtk83e3EPCpJQ0+0mOdozs8fI7Fw9iYwX251/h+mgUA/EDdrZGTSwJfQl8KoanW57JKftT/EBRVK1SUhHg175ZSFID/FB4XUOqQzrg==";

    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn1s/tvrIs+sXR3D5qhqkLaSP4RHjiY3q2ylBwBh3wNYCtxj/SreXm14QACdC4GyOqsfXeNgjSDtRndHfZXyNQg8O4EEPjVljD5okmD7tIahV9N499tgImxq1SqBUB62aPXOx0E8bC7yJz3e6pQ3z341cg03lGRBEUmw8T2X5/3OijWueskjk/LSn8+WyqesQ7oACe7jlLqfxaCkli/r9tT/j0PWwFqArhRCtZd+2INgmg3Yth06LX1LJxQl/vaorlqVJopo5/bR2V5NoCWCdWQVN3P1VqgeeGUtlK8pZ/4OavNX9QWWCFOGrsT3GIWUkjZaDTORrPLY65HFZTwt5SwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://xht685.natappfree.cc/api/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/api/alipay/toSuccess";
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        String body = "";

        // 修正后的参数（关键修改点）
        String bizContent = String.format(
                "{\"out_trade_no\":\"%s\"," +
                        "\"total_amount\":\"%.2f\"," +
                        "\"subject\":\"%s\"," +
                        "\"body\":\"%s\"," +
                        "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}",
                outTradeNo, totalAmount, subject, body
        );

        alipayRequest.setBizContent(bizContent);

        return alipayClient.pageExecute(alipayRequest).getBody();
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
